<?php
/**
 * DVelum project http://code.google.com/p/dvelum/ , http://dvelum.net
 * Copyright (C) 2011-2012  Kirill A Egorov kirill.a.egorov@gmail.com
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
/*
 * Startup time
 */
$scriptStart = microtime(true);
/*
 * Turning on output buffering
 */
ob_start();
/*
 * Connecting main configuration file
 */
$config = include './system/config/main.php';
/*
 * Including Autoloader class
 */
include $config['docroot'].'/system/library/Autoloader.php';
/*
 * Registering Autoloader
 */
spl_autoload_register(array('Autoloader' , 'load'));
/*
 * Setting autoloader config
 */
Autoloader::registerPaths($config['autoloader']['paths']);

if($config['autoloader']['useMap'])
	Autoloader::loadMap(include $config['autoloader']['map']);
	
if($config['autoloader']['usePackages'])
	Autoloader::loadPackagesConfig(include $config['autoloader']['packagesConfig']);
	
/*
 * Setting debuggers
 */
if($config['development']){
     Autoloader::$debug = true;
     Debug::setScriptStartTime($scriptStart);
}
/*
 * Cache initialization
 */
if($config['use_cache']){
    $cacheManager = new Zend_Cache_Manager();
    $cacheConfig = include $config['configs'].'cache_'.$config['use_cache'].'.php';
    $cacheManager->setCacheTemplate('system', $cacheConfig['system_cache']);
    $cacheManager->setCacheTemplate('data', $cacheConfig['data_cache']);
    $sc = $cacheManager->getCache('system');
    Config::setCacheCore($cacheManager->getCache('system'));
}
/**
 * System store initialization
 * @var Store_Local
 */
$systemStorage = Store::factory(Store::Local);
/**
 * Convert the data of main_config file 
 * in to the general form of configuration 
 * and save a reference for it (for convenience)
 * @var Config_Inline $inlineConfig
 */
$inlineConfig = Config::factory(Config::Inline, 'main');
$inlineConfig->setData($config);
$systemStorage->set('config_main', $inlineConfig);
/*
 * Starting the application
 */
if($config['use_cache']){
    $systemStorage->set('cache_manager', $cacheManager);
    Application::setDataCache($cacheManager->getCache('data'));
    Application::setSystemCache($cacheManager->getCache('system'));
}

$app = new Application(); 
$app->run();
/*
 * Clean the buffer and sends a response
 */
echo ob_get_clean();
/*
 * Printing debug information
 * dbhost
 */
if($config['development']){
	echo Debug::getStats();
}
exit;